//
//  CityChooserViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/20.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "CityChooserViewModel.h"

#import "CityChooserCellModel.h"

@interface CityChooserViewModel ()

@property (nonatomic, readwrite, copy) NSString* cityNum;
@property (nonatomic, readwrite, strong) ProvincesListData* currentPsListData;

@property (nonatomic, strong) RACCommand* finishCmd;
@property (nonatomic, strong) RACCommand* reqProvinceCmd;
@property (nonatomic, strong) RACCommand* reqCityCmd;

@end

@implementation CityChooserViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    self.cityNum = @"0";
    
    self.cityAreaIDs = @[];
    
    self.finishCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
      @strongify(self);
      
        return [self _citysSignal];
    }];
    
    self.reqProvinceCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        [self _reloadProvinceDatasByCityAreaIDs];
        
        return [self _requestProvinceSignal];
    }];
    
    self.reqCityCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
           
        return [self _requestCitySignal:input];
    }];
    
    DataManager* dm = [DataManager sharedDataManager];
    ProvincesListData* currentAddrDatas = [dm.addrData copy];
    self.currentPsListData = currentAddrDatas;
    
    self.provinceTVM = [[CityChooserTableViewModel alloc] initWithParams:@{}];
    self.provinceTVM.currentAddrDatas = currentAddrDatas;
    self.provinceTVM.cellHeight = 44.0f;
    self.provinceTVM.tag = 10;
    [[self.provinceTVM.selectCmd.executionSignals.switchToLatest deliverOnMainThread] subscribeNext:^(id x)
    {
        @strongify(self)
        
        NSIndexPath* indexPath = x;
        
        [self _reloadProvinceData:indexPath.row];
    }];
    [RACObserve(self.provinceTVM, dataSource) subscribeNext:^(id x) {
        
        @strongify(self);
        
        [self _updateCityNum];
    }];
    
    self.cityTVM = [[CityChooserTableViewModel alloc] initWithParams:@{}];
    self.cityTVM.currentAddrDatas = currentAddrDatas;
    self.cityTVM.cellHeight = 44.0f;
    self.cityTVM.tag = 11;
    [[self.cityTVM.selectCmd.executionSignals.switchToLatest deliverOnMainThread] subscribeNext:^(id x)
    {
        @strongify(self)
        
        NSIndexPath* indexPath = x;
        
        [self _chooseCityData:indexPath.row];
    }];
}

- (NSArray*)tableVMs
{
    return @[self.provinceTVM, self.cityTVM];
}

- (RACSignal*)_requestProvinceSignal
{
    @weakify(self);
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        @strongify(self);
        
        self.provinceTVM.areaID = @"0";
        
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        
        return nil;
    }];
}

- (RACSignal*)_requestCitySignal:(NSString*)areaID
{
    @weakify(self);
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                self.cityTVM.areaID = areaID;
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                
                return nil;
            }];
}

- (RACSignal*)_citysSignal
{
    @weakify(self);
    
    NSMutableArray* cityDatas = [NSMutableArray array];
    
    NSInteger count = self.provinceTVM.dataSource.count;
    for (NSInteger index=0; index<count; index++)
    {
        CityChooserCellModel* cellModel = self.provinceTVM.dataSource[index];
        
        ProvinceListData* pListData = cellModel.data;
        
        NSInteger subCount = pListData.datas.count;
        for (NSInteger subIndex=0; subIndex<subCount; subIndex++)
        {
            ProvinceListData* subPListData = pListData.datas[subIndex];
            
            if (subPListData.isChoose)
            {
                [cityDatas addObject:subPListData];
            }
        }
    }
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                [subscriber sendNext:cityDatas];
                [subscriber sendCompleted];
                
                return nil;
            }];
}

- (void)_updateCityNum
{
    NSInteger cityNum = 0;
    
    NSInteger count = self.provinceTVM.dataSource.count;
    for (NSInteger index=0; index<count; index++)
    {
        CityChooserCellModel* cellModel = self.provinceTVM.dataSource[index];
        
        ProvinceListData* pListData = cellModel.data;
        
        BOOL isSubChoose = NO;
        NSInteger subCount = pListData.datas.count;
        for (NSInteger subIndex=0; subIndex<subCount; subIndex++)
        {
            ProvinceListData* subPListData = pListData.datas[subIndex];
            
            if (subPListData.isChoose)
            {
                cityNum++;
                
                isSubChoose = YES;
            }
        }
        pListData.isChoose = isSubChoose;
    }
    
    self.cityNum = HCStrFormat(@"%d", (int)cityNum);
}

- (void)_reloadProvinceData:(NSInteger)cindex
{
    NSInteger count = self.provinceTVM.dataSource.count;
    for (NSInteger index=0; index<count; index++)
    {
        CityChooserCellModel* cellModel = self.provinceTVM.dataSource[index];
        
        ProvinceListData* pListData = cellModel.data;
        
        BOOL isChoose = NO;
        for (ProvinceListData* subPListData in pListData.datas)
        {
            if (subPListData.isChoose)
            {
                isChoose = YES;
                
                break;
            }
        }
        pListData.isChoose = isChoose;
        
        cellModel.isOn = index == cindex;
        
        if (index == cindex)
        {
            [self.reqCityCmd execute:pListData.areaID];
        }
    }
}

- (void)_chooseCityData:(NSInteger)cindex
{
    NSInteger count = self.cityTVM.dataSource.count;
    for (NSInteger index=0; index<count; index++)
    {
        CityChooserCellModel* cellModel = self.cityTVM.dataSource[index];
        
        if (index == cindex)
        {
            ProvinceListData* pListData = cellModel.data;
            pListData.isChoose = !pListData.isChoose;
            cellModel.isOn = pListData.isChoose;
            
            [self _updateCityNum];
        }
    }
}

- (void)_reloadProvinceDatasByCityAreaIDs
{
    DataManager* dm = [DataManager sharedDataManager];
    
    for (NSString* curAreaID in self.cityAreaIDs)
    {
        ProvinceListData* curPListData = [dm findCityDataBy:self.currentPsListData andAreaID:curAreaID];
        
        if (curPListData)
        {
            curPListData.isChoose = YES;
            
            ProvinceListData* curParentPListData = [dm findProvinceDataBy:self.currentPsListData andAreaID:curPListData.parentID];
            curParentPListData.isChoose = YES;
        }
    }
}

@end
